package com.mco.utils;

import lombok.val;

import java.util.HashSet;
import java.util.Set;

/**
 * hash 碰撞
 *
 * @author ：liao.wei
 * @date ：2023/9/23 14:29
 * @package : com.mco.utils
 */
public class HashCollisionUtil {
	
	public static void collision() {
//		System.out.println("BB".hashCode());
//		System.out.println("Aa".hashCode());
//
//		System.out.println("------------------------");
//
		Set<Integer> sets = new HashSet<>();
		int total = 0;
		for (int i = 0; i < 300000; i++) {
			Integer hashCode = new Object().hashCode();
			if (sets.contains(hashCode)) {
//				System.out.println("运行到" + i + "出现冲突，hashcode:" + hashCode);
				total++;
			}
			sets.add(hashCode);
		}
		System.out.println("300000 次冲突次数" + total);
	}
	
	public static void main(String[] args) {
		collision();
	}
}
